package blog.confirm;

import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import constant.Constants;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Producer {
    public static Connection createConnection() {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 设置IP地址、端口号
        factory.setHost(Constants.HOST);
        factory.setPort(Constants.PORT);
        // 设置虚拟机名称
        factory.setVirtualHost(Constants.VIRTUAL_HOST);
        // 设置用户名和密码
        factory.setUsername(Constants.USER_NAME);
        factory.setPassword(Constants.USER_PASSWORD);
        Connection connection = null;
        // 创建连接
        try {
            connection = factory.newConnection();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (TimeoutException e) {
            throw new RuntimeException(e);
        }finally {
            return connection;
        }
    }
}
